package top.kaoshanji.p2ch15b;

/**
 * 代码清单-p2ch15b-457页a：协作对象MyLatch
 * author kaoshanji
 * ^_^
 * create time 2025/2/24 17:21
 */
public class MyLatch {

    private int count;

    public MyLatch(int count) {
        this.count = count;
    }

    // 由主线程调用
    public synchronized void await() throws InterruptedException {
        while (count > 0) { // count 大于 0 说明还有未执行的线程
            wait();
        }
        System.out.println("...此时全部的线程都已经执行完了...");
    }

    // 由子线程调用
    public synchronized void countDown() {
        count--;
        if (count <= 0) { // count 小于等于 0 说明子线程都已经执行完了
            notifyAll();
        }
    }

}
